﻿Imports System
Imports System.IO
Imports ChatEngine.ServerManagement
Namespace ServerManagement
    Public Class ChannelListLoader
        Public Shared Sub SerializeObject(ByVal filename As String, ByVal col As ServerCollection, ByVal ExtraTypes() As System.Type)
            Try
                Dim s As New Xml.Serialization.XmlSerializer(GetType(ServerCollection), ExtraTypes)
                Dim writer As New IO.StreamWriter(filename)
                writer.Close()
            Catch x As Exception
                ' SharedVars.Services.Get(Of CoreFX.Interfaces.ILogging).AddItem("SerializeObject: " & x.ToString, CoreFX.Logging.MessageType.General)
            End Try
        End Sub
        Public Shared Sub SerializeObject(ByVal filename As String, ByVal col As ServerCollection)
            Try
                Dim s As New Xml.Serialization.XmlSerializer(GetType(ServerCollection))
                Dim writer As New IO.StreamWriter(filename)
                s.Serialize(writer, col)
                writer.Close()
            Catch x As Exception
                ' SharedVars.Services.Get(Of CoreFX.Interfaces.ILogging).AddItem("SerializeObject: " & x.ToString, CoreFX.Logging.MessageType.General)
            End Try
        End Sub
        Public Shared Function DeserializeObject(ByVal filename As String, ByVal ExtraTypes() As System.Type) As ServerCollection
            Try
                Dim fs As New IO.FileStream(filename, FileMode.Open)
                Dim w As New Xml.Serialization.XmlSerializer(GetType(ServerCollection), ExtraTypes)
                Dim g As ServerCollection = CType(w.Deserialize(fs), ServerCollection)
                fs.Close()
                Return g
            Catch x As Exception
                ' SharedVars.Services.Get(Of CoreFX.Interfaces.ILogging).AddItem("SerializeObject: " & x.ToString, CoreFX.Logging.MessageType.General)
                Return Nothing
            End Try
        End Function
        Public Shared Function DeserializeObject(ByVal filename As String) As ServerCollection
            Try
                Dim StartTime As DateTime = Format(DateTime.Now, "hh:mm:ss")
                Dim EndTime As DateTime
                Dim ElaspsedTime As TimeSpan
                Dim fs As New IO.FileStream(filename, FileMode.Open)
                Dim w As New Xml.Serialization.XmlSerializer(GetType(ServerCollection))
                Dim g As ServerCollection = CType(w.Deserialize(fs), ServerCollection)
                fs.Close()
                EndTime = Format(DateTime.Now, "hh:mm:ss")
                ElaspsedTime = StartTime.Subtract(EndTime)
                Return g
            Catch x As Exception
                ' SharedVars.Services.Get(Of CoreFX.Interfaces.ILogging).AddItem("SerializeObject: " & x.ToString, CoreFX.Logging.MessageType.General)
                Return Nothing
            End Try
        End Function
        Public Sub New()

        End Sub
    End Class
End Namespace